﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;


namespace GPS.Controllers.GPS_NghiepVu
{
    public class GPS_KeHoachSatHachController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public ActionResult Index(ViewModelDKHSH SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            var entities = _db.Filter<DKHSH>(o => (SearchModel.pKHSH_MA == null || o.KHSH_MA.StartsWith(SearchModel.pKHSH_MA))
                                                             && (SearchModel.pKHSH_SH_MA == null || o.KHSH_SH_MA.Contains(SearchModel.pKHSH_SH_MA))
                                                             && (SearchModel.pKHSH_DVGTVT_MA == null || o.KHSH_DVGTVT_MA == SearchModel.pKHSH_DVGTVT_MA)
                                                             && (SearchModel.pKHSH_NKY == null || o.KHSH_NKY == SearchModel.pKHSH_NKY)
                                                             && (SearchModel.pKHSH_NGAYSH_TU == null || (o.KHSH_NGAYSH >= SearchModel.pKHSH_NGAYSH_TU))
                                                             && (SearchModel.pKHSH_NGAYSH_DEN == null || (o.KHSH_NGAYSH <= SearchModel.pKHSH_NGAYSH_DEN))
                                                             );

            var model = new List<DKHSH>();
            foreach (var currentEntity in entities)
            {
                model.Add(Mapper.Map<DKHSH>(currentEntity));
            }

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);

            return View(SearchModel);
        }

        public ActionResult Create()
        {
            initialCategoryCreateAction();
            return View(new CrudModelDKHSH());
        }

        [HttpPost]
        public ActionResult Create(CrudModelDKHSH model)
        {
            if (ModelState.IsValid)
            {

                DKHSH _model = new DKHSH();
                GPS.COMMON.Helpers.CopyObject<DKHSH>(model, ref _model);
                _model.ID = EFSequence.GetNextValue("SEQ_DKHSH_ID");
                _db.Insert<DKHSH>(_model);

                return RedirectToAction("Index");
            }

            return View(model);
        }

        public ActionResult Edit(decimal id)
        {
            DKHSH model = _db.GetOne<DKHSH>(o => o.ID == id);

            CrudModelDKHSH _model = new CrudModelDKHSH();
            GPS.COMMON.Helpers.CopyObject<CrudModelDKHSH>(model, ref _model);
           
            initialCategoryEditAction(_model);
            return View(_model);
        }

        [HttpPost]
        public ActionResult Edit(CrudModelDKHSH model)
        {
            if (ModelState.IsValid)
            {
                var _model = _db.GetOne<DKHSH>(o => o.ID==model.ID);
                GPS.COMMON.Helpers.CopyObject<DKHSH>(model, ref _model);
               
                _db.Update<DKHSH>(_model);
                return RedirectToAction("Index");
            }

            return View(model);
        }

        public ActionResult Delete(decimal id)
        {
            _db.Delete<DKHSH>(o => o.ID == id);
            return RedirectToAction("Index");
        }


        public JsonResult AutoCompleteHV_NOITT_DVHC_MA(string term)
        {
            var result = _db.Filter<SDONVIHC>(o => o.DVHC_MA.Contains(term));
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult getDSH(string p_DVGTVT_MA)
        {
            var districts = _db.Filter<DSH>(d => d.SH_DVGTVT_MA == p_DVGTVT_MA).Select(a => "<option value='" + a.SH_MA + "'>" + a.SH_TENKYSH + "</option>");

            return Content(string.Join("", districts));
        }

        [HttpPost]
        public ActionResult getKHSH_MA(string p_DVGTVT_MA, string p_KHSH_SH_MA)
        {

            string _YY_NOW = DateTime.Now.Year.ToString().Substring(2, 2);
            string strKHSH_MA = p_KHSH_SH_MA + DateTime.Now.ToString("yyyyMMdd");

            return Content(string.Join("", strKHSH_MA));
        }

        [HttpPost]
        public JsonResult getKHSH_SH_MA_EXISTS( string p_KHSH_MA)
        {
            var _exists=_db.EntityExists<DKHSH>(o=>o.KHSH_MA==p_KHSH_MA);
            return Json(_exists, JsonRequestBehavior.AllowGet);
        }

        #region detail
        public ActionResult Detail(decimal id)
        {
            DKHSH model = _db.GetOne<DKHSH>(o => o.ID==id);
            CrudModelDKHSH _model = new CrudModelDKHSH();
            GPS.COMMON.Helpers.CopyObject<CrudModelDKHSH>(model, ref _model);
            initialCategoryDetailAction(_model);
            return PartialView("_Detail", _model);
        }
        public void initialCategoryDetailAction(CrudModelDKHSH view)
        {
            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.KHSH_SH_MAs=new SelectList(_db.GetAll<DSH>(),"SH_MA", "SH_TENKYSH");
        }
        #endregion

        public void initialCategorySearchAction(ViewModelDKHSH viewModel)
        {
            // ApplicationUser curUser = getCurrentUser();
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.KHSH_SH_MAs = new SelectList(_db.GetAll<DSH>(), "SH_MA", "SH_TENKYSH");

        }

        public void initialCategoryCreateAction()
        {
            // ApplicationUser curUser = getCurrentUser();
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.KHSH_SH_MAs = new SelectList(_db.GetAll<DSH>(), "SH_MA", "SH_TENKYSH");

        }

        public void initialCategoryEditAction(CrudModelDKHSH view)
        {
            // ApplicationUser curUser = getCurrentUser();
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.KHSH_SH_MAs = new SelectList(_db.GetAll<DSH>(), "SH_MA", "SH_TENKYSH");

        }
	}
}